<template>
  <view class="container">
    <unicloud-db ref="udb" :where="`user_id == $cloudEnv_uid && delState != true`" :orderby="`publish_date desc`"
      v-slot:default="{data, pagination, loading, hasMore, error}" :collection="collectionList"
      field="user_id,title,description,province,content,article_status,last_comment_user_id,picurls,publish_date,last_modify_date">
      <view v-if="error">{{error.message}}</view>
      <view v-else-if="data">
        <uni-list>
          <uni-list-item v-for="(item, index) in data" :key="index" showArrow :clickable="true"
            @click="handleItemClick(item._id)">
            <template v-slot:body>
              <uni-section class="mb-10" :title="'标题：'+item.title" :sub-title="formatTime(item.publish_date)">
              </uni-section>
            </template>
          </uni-list-item>
        </uni-list>
      </view>
      <uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
    </unicloud-db>
    <!-- <uni-fab ref="fab" horizontal="right" vertical="bottom" :pop-menu="false" @fabClick="fabClick" /> -->
  </view>
</template>

<script>
  import dayjs from 'dayjs'
  import relativeTime from 'dayjs/plugin/relativeTime'
  const db = uniCloud.database()
  export default {
    data() {
      return {
        collectionList: "pyq_article",
        loadMore: {
          contentdown: '',
          contentrefresh: '',
          contentnomore: ''
        }
      }
    },
    mounted() {
      dayjs.extend(relativeTime)
    },
    onPullDownRefresh() {
      this.$refs.udb.loadData({
        clear: true
      }, () => {
        uni.stopPullDownRefresh()
      })
    },
    onReachBottom() {
      this.$refs.udb.loadMore()
    },
    methods: {
      formatTime(time) {
        return dayjs(time).format("YY/MM/DD-HH点mm分")
      },
      handleItemClick(id) {
        uni.navigateTo({
          url: '/pages/detail/detail?id=' + id
        })
      },
      fabClick() {
        // 打开新增页面
        uni.navigateTo({
          url: './add',
          events: {
            // 监听新增数据成功后, 刷新当前页面数据
            refreshData: () => {
              this.$refs.udb.loadData({
                clear: true
              })
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .container {
    :deep(.uni-list-item__container) {
      padding: 0;
    }
  }
</style>
